package com.onix.scalecamerapreview.translate.OCR;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;

import java.util.List;

public class MsLineView extends GraphicOverlay.Graphic {
    private final MsOcrBean.RegionsBean.LinesBean linesBean;
    private final Paint rectPaint;
    private final Paint mTextPaint;

    public MsLineView(GraphicOverlay overlay, MsOcrBean.RegionsBean.LinesBean linesBean) {
        super(overlay);
        this.linesBean = linesBean;
        //区域
        rectPaint = new Paint();
        rectPaint.setColor(Color.BLUE);
        rectPaint.setStyle(Paint.Style.STROKE);
        rectPaint.setStrokeWidth(2);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextSize(40);
    }

    @Override
    public void draw(Canvas canvas) {
        String boundingBox = linesBean.getBoundingBox();
        String[] split = boundingBox.split(",");
        RectF rect = new RectF(Float.valueOf(split[0]),Float.valueOf(split[1]),Float.valueOf(split[0])+Float.valueOf(split[2]),Float.valueOf(split[1])+Float.valueOf(split[3]));

        List<MsOcrBean.RegionsBean.LinesBean.WordsBean> words = linesBean.getWords();
        StringBuilder stringBuilder = new StringBuilder();
        for (MsOcrBean.RegionsBean.LinesBean.WordsBean word : words) {
            stringBuilder.append(word.getText());
        }
        if(stringBuilder.toString().equals("并不是因为你有房有车")) {
            rectPaint.setStyle(Paint.Style.FILL);
            rectPaint.setColor(Color.WHITE);
            canvas.drawRect(rect,rectPaint);

            String content = "not because you have room and car";
            Rect textBound = new Rect();
            mTextPaint.getTextBounds(content, 0, content.length(), textBound);



            int availableWidth = (int) rect.width();
            int currentWdith = textBound.width();
            float cTextSize = mTextPaint.getTextSize();
            Log.d("AAA","cTextSize "+cTextSize +"currentWdith "+currentWdith+" - availableWidth "+availableWidth);
            while (currentWdith>availableWidth){
                cTextSize = cTextSize-0.5f;
                mTextPaint.setTextSize(cTextSize);//这里传入的单位是px
                mTextPaint.getTextBounds(content, 0, content.length(), textBound);
                currentWdith = textBound.width();
                Log.d("AAA","currentWdith "+currentWdith);
            }
            cTextSize = cTextSize+0.5f;
            mTextPaint.setTextSize(cTextSize);
            Log.d("AAA","cTextSize之后 "+cTextSize);

            Paint.FontMetricsInt anInt = mTextPaint.getFontMetricsInt();
            //字体基线
            int dy = (anInt.bottom - anInt.top) / 2 - anInt.bottom;
            canvas.drawText(content, rect.left,  dy+rect.top+(rect.bottom-rect.top)/2, mTextPaint);
        }else {
            rectPaint.setStyle(Paint.Style.STROKE);
            rectPaint.setColor(Color.BLUE);
            canvas.drawRect(rect,rectPaint);
        }

    }




}
